import 'package:flutter/material.dart';

import 'package:get/get.dart';
import '../../../../models/message_model.dart';
import '../controllers/code_login_step_two_controller.dart';
import '../../../../utils/screenAdapter.dart';
import '../../../../common/widget/logo.dart';
import '../../../../common/widget/codeButton.dart';
// import '../../../../common/widget/pinInput.dart';
import 'package:pin_code_fields/pin_code_fields.dart';

class CodeLoginStepTwoView extends GetView<CodeLoginStepTwoController> {
  const CodeLoginStepTwoView({super.key});
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      appBar: AppBar(
        // elevation: 0,
        title: const Text('手机号快速登录'),
        centerTitle: true,
      ),
      body: ListView(
        padding: EdgeInsets.fromLTRB(
          ScreenAdapter.width(30),
          ScreenAdapter.height(100),
          ScreenAdapter.width(30),
          ScreenAdapter.height(30),
        ),
        children: [
          Logo(),
          SizedBox(height: ScreenAdapter.height(160)),
          // Row(
          //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
          //   children: [
          //   PinInput(isFirst: true, controller: controller.firstField, autoFocus: true),
          //   PinInput(isFirst:false, controller: controller.secondField, autoFocus: false),
          //   PinInput(isFirst:false, controller: controller.thirdField, autoFocus: false),
          //   PinInput(isFirst:false, controller: controller.fourthField, autoFocus: false),
          //   PinInput(isFirst:false, controller: controller.fifthField, autoFocus: false),
          //   PinInput(isFirst:false, controller: controller.sixthField, autoFocus: false),
          // ],),
          PinCodeTextField(
            controller: controller.codeController,
            pastedTextStyle: TextStyle(color: Colors.black),
            autoFocus: true,
            keyboardType: TextInputType.number,
            appContext: context,
            length: 6,
            obscureText: false,
            animationType: AnimationType.fade,
            pinTheme: PinTheme(
              shape: PinCodeFieldShape.box, // 验证码输入框的形状样式
              borderRadius: BorderRadius.circular(5),
              fieldHeight: 50,
              fieldWidth: 40,
              // 边框颜色
              activeColor: Colors.orange,
              inactiveColor: Colors.black12,
              selectedColor: Colors.orange,
              // 格子的背景颜色
              activeFillColor: Colors.white,
              inactiveFillColor: Colors.white,
              selectedFillColor: Colors.orange,
            ),
            dialogConfig: DialogConfig(
              affirmativeText: '确定',
              negativeText: '取消',
              dialogTitle: '提示',
              dialogContent: "确定黏贴",
            ), // 黏贴对话框配置
            animationDuration: Duration(milliseconds: 300),
            enableActiveFill: true,
            onCompleted: (v) {
              print("Completed");
            },
            onChanged: (value) {
              print(value);
            },
            beforeTextPaste: (text) {
              print("Allowing to paste $text");
              //if you return true then it will show the paste confirmation dialog. Otherwise if false, then nothing will happen.
              //but you can show anything you want here, like your pop up saying wrong paste format or etc
              return true;
            },
          ),
          SizedBox(height: ScreenAdapter.height(60)),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              Obx(
                () => controller.seconds.value == 0
                    ? TextButton(
                        onPressed: (){
                          controller.sendCode(Get.arguments["tel"]);
                        },
                        child: Text(
                          "重新发送验证码",
                          style: TextStyle(color: Colors.black54),
                        ),
                      )
                    : TextButton(
                        onPressed: null,
                        child: Text(
                          "重新发送验证码(${controller.seconds.value})",
                          style: TextStyle(color: Colors.black54),
                        ),
                      ),
              ),
              TextButton(
                onPressed: () {},
                child: Text("帮助", style: TextStyle(color: Colors.black54)),
              ),
            ],
          ),

          CodeButton(
            textContent: "登录",
            onPressed: ()  async {
              FocusScope.of(context).requestFocus(FocusNode()); // 收起软键盘
              MessageModel messageModel = await  controller.goCodeLogin();
              if(messageModel.success ==true ) {
                // Get.offAllNamed("/tabs", arguments: {"index": 4});
                Get.back(); // 直接返回上一级页面时，上一节页面的controller不会重新onInit,所以得在此页面的onClose调用上一级初始化需要完成的的方法
              }else {
                Get.snackbar("提示", "${messageModel.message}",);
              }
            },
          ),
        ],
      ),
    );
  }
}
